package com.gwclh.personnel.service.impl;

import cn.hutool.core.lang.Assert;
import com.gwclh.personnel.bean.ResultVo;
import com.gwclh.personnel.service.IFileUploadService;
import com.gwclh.personnel.utils.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;


@Service
@Slf4j
public class FileUploadServiceImpl implements IFileUploadService {
    @Autowired
    OssUtil ossUtil;

    @Override
    public ResultVo uploadSingleImage(MultipartFile file, Integer type) {
        String url = ossUtil.uploadOneImage(file, type);
        Assert.notNull(url, "上传失败");
        String finalUrl = url.split("\\?")[0];
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("url", finalUrl);
        return ResultVo.success(resultMap);

    }

    @Override
    public ResultVo uploadMutiImage(MultipartFile[] files) {
            HashMap<String, Object> resultMap = new HashMap<>();
        try {
            String urls = ossUtil.uploadImageList(Arrays.asList(files));
            resultMap.put("urls",urls);
        } catch (Exception e) {
            return ResultVo.fail(500, "上传失败");
        }
        return ResultVo.success(resultMap);
    }
}
